/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		Rect.h

					E-mail:		arise_engine@mail.ru

					Origin by:	Haaf's Game Engine 1.5, 
								Copyright (c) 2003-2004 Relish Games
								http://hge.relishgames.com


created:			27.01.2008
last changes:		27.01.2008
*****************************************************************************************/
#pragma once

//	Rect is a helper class for bounding box calculations and collision detection. 


class Rect
{
private:

	bool	m_is_clean;

public:
	
	float x1;	//	The top-left corner X-coordinate
	float y1;	//	The top-left corner Y-coordinate
	float x2;	//	The bottom-right corner X-coordinate
	float y2;	//	The bottom-right corner Y-coordinate

	Rect(float _x1, float _y1, float _x2, float _y2) 
	{
		x1	= _x1; 
		y1	= _y1; 
		x2	= _x2; 
		y2	= _y2; 
		m_is_clean = false; 
	}
	Rect() 
	{
		x1	= 0; 
		y1	= 0; 
		x2	= 0; 
		y2	= 0; 
		m_is_clean = true;
	}

	void Clear()				
	{
		m_is_clean = true;
	}
	
	void Encapsulate(float x, float y)
	{
		//	Encapsulates a point into bounding box.
		if(m_is_clean) 
		{
			x1 = x2 = x;
			y1 = y2 = y;
			m_is_clean = false;
		} 
		else 
		{
			if(x < x1) x1 = x;
			if(x > x2) x2 = x;
			if(y < y1) y1 = y;
			if(y > y2) y2 = y;
		}
	}
	
	bool TestPoint(float x, float y) const
	{
		//	Tests if a point lies within bounding box
		if(x>=x1 && x<x2 && y>=y1 && y<y2) return true;
		return false;
	}
	
	bool Intersect(const Rect* rect) const
	{
		//	Tests two bounding boxes for an intersection
		if(fabs(x1+x2 - rect->x1-rect->x2) < (x2-x1 + rect->x2-rect->x1)) 
		{
			if(fabs(y1+y2 - rect->y1-rect->y2) < (y2-y1 + rect->y2-rect->y1)) 
				return true;
		}
		return false;
	}

	void Set(float _x1, float _y1, float _x2, float _y2) 
	{ 
		//	Sets bounding box to the specified screen coordinates
		x1 = _x1; 
		x2 = _x2; 
		y1 = _y1; 
		y2 = _y2; 
		m_is_clean = false; 
	}
	
	void SetRadius(float x, float y, float r) 
	{ 
		//	Sets up bounding box, based on it's center coordinates and radius
		x1 = x-r; 
		x2 = x+r; 
		y1 = y-r; 
		y2 = y+r; 
		m_is_clean = false; 
	}

	bool IsClean() const {return m_is_clean;}
};
